//
//  ReadColorCommand.m
//  Command
//
//  Created by Hechun on 2020/11/7.
//

#import "ReadColorCommand.h"

// util
#import "CommandManager.h"
#import "StorageTool.h"

@interface ReadColorCommand ()

@property (nonatomic, strong) NSString *newsId;

@property (nonatomic, assign) BOOL isSaveNewsId;

/// 自定义普通颜色
@property (nonatomic, strong) UIColor *normalColor;

/// 自定义已读颜色
@property (nonatomic, strong) UIColor *readColor;

/// 当前颜色
@property (nonatomic, strong) UIColor *color;

@end

@implementation ReadColorCommand

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.normalColor = [UIColor blackColor];
        self.readColor = [UIColor grayColor];
    }
    return self;
}

- (void)execute {
    if (self.isSaveNewsId) {
        self.color = self.readColor;
        [StorageTool saveNewsId:self.newsId];
    } else {
        BOOL isContain = [StorageTool isContainNewsId:self.newsId];
        self.color = isContain ? self.readColor : self.normalColor;
    }
    
    [self done];
}

- (void)customNormalColor:(UIColor *)normalColor readColor:(UIColor *)readColor {
    self.normalColor = normalColor;
    self.readColor = readColor;
}

- (void)saveNewsId:(NSString *)newsId finish:(ReadColorCommandCallBack)finish {
    self.newsId = newsId;
    self.isSaveNewsId = YES;
    [CommandManager executeCommand:self completion:^(Command * _Nonnull cmd) {
        if (finish) {
            finish(self.color);
        }
    }];
}

- (void)colorWithNewsId:(NSString *)newsId finish:(ReadColorCommandCallBack)finish {
    self.newsId = newsId;
    self.isSaveNewsId = NO;
    [CommandManager cancelCommand:self];
    [CommandManager executeCommand:self completion:^(Command * _Nonnull cmd) {
        if (finish) {
            finish(self.color);
        }
    }];
}


@end
